# !/usr/local/bin/ruby -w 
# authenticate.rb
#
# This program realizes the AdministratorInterface Subsystem.
# It gives the Administrator access to View Database after authentication.
# The Administrator is given privilage to view database based on selected parameters
#
# Authors:: Ronak Shah (mailto: ronshah@eden.rutgers.edu )
# License:: Distributes under the same terms as Ruby
#
# This class has method check () to authenticate the administrator by checking 
# its login details (username and password)

class Authenticate

    attr_accessor :username, :password

# Remembers the initial username and password
    def initialize(username, password)
       @username = username
       @password = password

    end

# Method to check to see if the the input login details are same as those stored
    def check(user)

require "mysql"

dbname = "tripti"

# Connect to the database
m = Mysql.new("idb1", "orbit", "orbit")
# r = m.query("CREATE DATABASE #{dbname}")

# Select the database to be used
m.select_db(dbname)


       @username= @username.strip!
       @password= @password.strip!

       if (@username!= user.username || @password!=user.password )
         puts "Invalid username or password"
       else
         puts "Welcome"

            while(i=1)

                def call(s)
                   s = s.strip!
                     if 
                       s == 'yes'
                       i=1
                     else
                       i= 0 
                       exit
                     end
                end


# Privilage given to Administrator to view Database based on given Parameters
         puts "Please Enter one of the following options to view enteries in Database \n
               1. To view enteries less than particular time stamp: Enter time as hh:mm:ss < \n
               2. To view enteries greater than particular time stamp: Enter time as hh:mm:ss > \n
               3. To view enteries at that particulas time stamp: Enter time as hh:mm:ss = \n
               4. To view enteries of a particular sender MAC address: aa:bb:cc:dd:ee:ff \n"
               

         str=gets
         puts str
           temp = []
           temp = str.split(" ")

	case str

             when /\d\d:\d\d:\d\d </
                puts "You have requested to view DB less than this time stamp"
                #Query for DB as per time stamp
		result = m.query("SELECT * FROM packet WHERE timestamp < '#{temp[0]}';")
		result.each do |row|
                row.each do |column|
		puts " #{column} "
                end
		puts
		end
                puts "Do you want to continue: enter yes or no ?"
                s= gets
                call(s)


	     when /\d\d:\d\d:\d\d >/
                puts "You have requested this input to view DB greater than this time"
                #Query for DB as per time stamp
                result =  m.query("SELECT * FROM packet WHERE timestamp > '#{temp[0]}';")
                result.each do |row|
                row.each do |column|
                puts " #{column} "
                end
		puts
		end
                puts "Do you want to continue: enter yes or no ?"
                s= gets
                call(s)


	    when /\d\d:\d\d:\d\d =/
                puts "You have requested to view db as per time equal to t"
                #Query for db as per time equal to given time
                result = m.query("SELECT * FROM packet WHERE timestamp = '#{temp[0]}';")
	        result.each do |row|
	        row.each do |column|
	        puts " #{column} "
	        end
		puts
		end
                puts "Do you want to continue: enter yes or no ?"
                s= gets
                call(s)


	     when /\d\d:\d\d:\d\d:\d\d:\d\d:\d\d/
                puts "Sender Mac Address"
                #Query for db as per Mac Address
                result =  m.query("SELECT * FROM packet WHERE dst_addr = '#{temp[0]}';")
                result.each do |row|
                row.each do |column|
                puts " #{column} "
                end
                puts
		end
                puts "Do you want to continue: enter yes or no ?"
                s= gets
                call(s)

             else
                puts "Please try again and provide valid input"
                puts "Do you want to continue: enter yes or no ?"
                s= gets
                call(s)

             end   #end of case
          
           end   # end of while

        end # end of if

	  #Close the connection to database
	  m.close

    end # end of def

end #end of class


# Administrator Login Details: Username: admin, Password: star
user = Authenticate.new("admin", "star")

#Enter Login Details
user_check=Authenticate.new("a","b")
puts "Enter Username"
user_check.username=gets
puts "Enter Password"
user_check.password=gets
user_check.check(user)

